package com.webserver.core;


import com.webserver.http.EmptyRequestException;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;


public class ClientHandler implements Runnable{
    private Socket socket;

    public ClientHandler(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        //1解析请求
        try {
            HttpServletRequest request=new HttpServletRequest(socket);
            HttpServletResponse response=new HttpServletResponse(socket);

            DispatcherServlet servlet=new DispatcherServlet();
            servlet.service(request,response);

            response.response();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (EmptyRequestException e) {
            //仅仅为了忽略浏览器空请求.
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //2处理请求
        //3发送响应
    }
    

}
